package com.demomaster.quickcompat.listener;

import android.view.View;

import java.util.HashMap;
import java.util.Map;

public class QuickClickBinder {
    private View.OnClickListener mlistener;
    private Map<Integer, View.OnClickListener> onClickListenerMap;
    public QuickClickBinder(){
        onClickListenerMap = new HashMap<>();
        mlistener  = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onClickListenerMap.containsKey(v.getId())){
                    onClickListenerMap.get(v.getId()).onClick(v);
                }
            }
        };
    }
    public void bindSingleClick(View view, View.OnClickListener listener){
        OnSingleClickListener onSingleClickListener  = new OnSingleClickListener() {
            @Override
            public void onClickEvent(View v) {
                if(listener!=null){
                    listener.onClick(v);
                }
            }
        };
        view.setOnClickListener(onSingleClickListener);
    }
    public void bindClick(View view, View.OnClickListener listener){
        view.setOnClickListener(listener);
    }
    public void unBingClick(View view){
        onClickListenerMap.remove(view.getId());
        view.setOnClickListener(null);
    }
    public void destroy(){
        onClickListenerMap.clear();
        onClickListenerMap = null;
        mlistener = null;
    }
}
